package com.qxl.forest.common.constants.factory;

import com.qxl.forest.common.constants.cache.Cache;
import com.qxl.forest.common.constants.cache.CacheKey;
import com.qxl.forest.business.persistence.dao.DeptDao;
import com.qxl.forest.business.persistence.dao.RoleDao;
import com.qxl.forest.business.persistence.dao.UserDao;
import com.qxl.forest.business.persistence.entity.Dept;
import com.qxl.forest.business.persistence.entity.Role;
import com.qxl.forest.common.util.ToolUtil;
import com.qxl.forest.util.Convert;
import com.qxl.forest.util.SpringContextHolder;
import com.qxl.forest.util.StrKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author: qiuxinlin
 * @Dercription: 常量生产工厂
 * @Date: 10:45 2017/11/24
 */
@Component
@DependsOn("springContextHolder")
public class ConstantFactory implements IConstantFactory {

    @Autowired
    UserDao userDao;
    @Autowired
    DeptDao deptDao;
    @Autowired
    RoleDao roleDao;

    public static IConstantFactory me() {
        return SpringContextHolder.getBean("constantFactory");
    }

    @Override
    @Cacheable(value = Cache.CONSTANT, key = "'" + CacheKey.ROLES_NAME + "'+#roleIds")
    public String getRoleName(String roleIds) {
        Integer[] roles = Convert.toIntArray(roleIds);
        StringBuilder sb = new StringBuilder();
        for (int roleId : roles) {
            Role role = roleDao.findOne(roleId);
            if (ToolUtil.isNotEmpty(role) && ToolUtil.isNotEmpty(role.getName())) {
                sb.append(role.getName()).append(",");
            }
        }
        return StrKit.removeSuffix(sb.toString(), ",");
    }

    @Override
    @Cacheable(value = Cache.CONSTANT, key = "'" + CacheKey.SINGLE_ROLE_NAME + "'+#roleId")
    public String getSingleRoleName(Integer roleId) {
        if (0 == roleId) {
            return "--";
        }
        Role role = roleDao.findOne(roleId);
        if (ToolUtil.isNotEmpty(role) && ToolUtil.isNotEmpty(role.getName())) {
            return role.getName();
        }
        return "";
    }

    @Override
    @Cacheable(value = Cache.CONSTANT, key = "'" + CacheKey.SINGLE_ROLE_TIP + "'+#roleId")
    public String getSingleRoleCode(Integer roleId) {
        if (0 == roleId) {
            return "--";
        }
        Role role = roleDao.findOne(roleId);
        if (ToolUtil.isNotEmpty(role) && ToolUtil.isNotEmpty(role.getName())) {
            return role.getCode();
        }
        return "";
    }

    @Override
    @Cacheable(value = Cache.CONSTANT, key = "'" + CacheKey.DEPT_NAME + "'+#deptId")
    public String getDeptName(Integer deptId) {
        Dept dept = deptDao.findOne(deptId);
        if (!ObjectUtils.isEmpty(dept) && !StringUtils.isEmpty(dept.getFullname())) {
            return dept.getFullname();
        }
        return "";
    }

    @Override
    public List<Integer> getSubDeptId(Integer deptid) {
        List subDeptIds = deptDao.findIdsByPid(deptid);
        return subDeptIds;
    }

    @Override
    @Cacheable(value = Cache.CONSTANT, key = "'" + CacheKey.SINGLE_ROLE_PNAME + "'+#roleId")
    public String getRolePnameByPid(Integer rolePid) {
        Role role = roleDao.findOne(rolePid);
        if (ToolUtil.isNotEmpty(role)) {
            return role.getName();
        }
        return "";
    }

    @Override
    @Cacheable(value = Cache.CONSTANT, key = "'" + CacheKey.DEPT_NAME + "'+#roleId")
    public String getDeptNameByRoleId(Integer roleId) {
        Role role = roleDao.findOne(roleId);
        if (ToolUtil.isNotEmpty(role) && ToolUtil.isNotEmpty(deptDao.findOne(role.getDeptid()))) {
            return deptDao.findOne(role.getDeptid()).getFullname();
        }
        return "";
    }

}
